package manager.com.ithei.edu.info.manager.controller;

import manager.com.ithei.edu.info.manager.domain.Student;
import manager.com.ithei.edu.info.manager.service.StudentService;

import java.io.IOException;
import java.text.ParseException;
import java.util.Scanner;

public abstract class FatherStudentController {
    private StudentService studentService = new StudentService();

    private Scanner sc = new Scanner(System.in);

    // 开启学生管理系统, 并展示学生管理系统菜单
    public final void start() throws IOException, ParseException {
        studentLoop:
        while (true) {
            System.out.println("--------欢迎来到 <学生> 管理系统--------");
            System.out.println("请输入您的选择: 1.添加学生  2.删除学生  3.修改学生  4.查看学生  5.退出");
            String choice = sc.next();
            switch (choice) {
                case "1":
                    System.out.println("添加");
                    addStudent();
                    break;
                case "2":
                    System.out.println("删除");
                    deleteStudentById();
                    break;
                case "3":
                    System.out.println("修改");
                    //setStudentById();
                    setStudentByIdTwo();
                    break;
                case "4":
                    System.out.println("查询");
                    findAllStudents();
                    break;
                case "5":
                    System.out.println("感谢您使用学生管理系统, 再见!");
                    break studentLoop;
                default:
                    System.out.println("您的输入有误, 请重新输入");
                    break;
            }
        }
    }

    //第一种修改学生对象的方法
    private final void setStudentByIdTwo() throws ParseException {
        String id = getId(true);
        //录入设置的学生其他信息
        Student stu = inputStudentInfo(id);
        //根据id设置学生信息
        studentService.setStudentByIdTwo(id, stu);
    }

    //第二种修改学生对象的方法
//    private final void setStudentById() {
//        String id;
//        while(true){
//            System.out.println("请输入要修改的学生的id:");
//            id=sc.next();
//            boolean flag=studentService.isExists(id);
//            if(!flag){
//                System.out.println("该学生id不存在,请重新输入:");
//            }else{
//                break;
//            }
//        }
//        Student stu=inputStudentInfo(id);
//        boolean bl=studentService.setStudentById(stu);
//        if(bl){
//            System.out.println("修改成功!");
//        }else{
//            System.out.println("修改失败!");
//        }
//    }

    private final void deleteStudentById() {
        String id;
        while (true) {
            System.out.println("请输入id:");
            id = sc.next();
            boolean flag = studentService.isExists(id);
            if (!flag) {
                System.out.println("输入不正确,请重新输入:");
            } else {
                break;
            }
        }
        //第一种删除学生对象的方法
        studentService.deleteStudentByIdTwo(id);
        System.out.println("删除成功!");
    }

    public final void findAllStudents() {
        Student[] stu = StudentService.findAllStudents();
        if (stu == null) {
            System.out.println("查无信息!!!");
        } else {
            System.out.println("姓名\t\t学号\t\t年龄\t\t生日\t\t地址\t\t");
            for (int i = 0; i < stu.length; i++) {
                if (stu[i] != null) {
                    System.out.println(stu[i].getName() + "\t\t" + stu[i].getSid() + "\t\t" + stu[i].getAge() + "\t\t" + stu[i].getBirthday() + "\t\t" + stu[i].getAddress());
                }
            }
        }
    }

    // 添加学生方法
    public final void addStudent() throws ParseException {
        // StudentService studentService = new StudentService();
        // 1. 键盘接收学生信息
        String id = getId(false);
        Student stu = inputStudentInfo(id);
        // 3. 将学生对象,传递给StudentService(业务员)中的addStudent方法
        boolean result = studentService.addStudent(stu);
        // 4. 根据返回的boolean类型结果, 在控制台打印成功\失败
        if (result) {
            System.out.println("添加成功");
        } else {
            System.out.println("添加失败");
        }
    }

    // 键盘录入学生信息
    public abstract Student inputStudentInfo(String id) throws ParseException;

    public final String getId(boolean bool) {
        String id;
        while (true) {
            System.out.println("请输入id:");
            id = sc.next();
            boolean flag = studentService.isExists(id);
            if (flag ^ bool) {
                System.out.println("请重新输入");
            } else {
                break;
            }
        }
        return id;
    }


}
